"""重构数据库模型以匹配RuoYi结构

Revision ID: db3594145458
Revises:
Create Date: 2025-09-29 11:41:57.434885

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'db3594145458'
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
    # 租户表
    op.create_table('sys_tenant',
        sa.Column('id', sa.BigInteger(), nullable=False, comment='id'),
        sa.Column('tenant_id', sa.String(length=20), nullable=False, comment='租户编号'),
        sa.Column('contact_user_name', sa.String(length=20), nullable=True, comment='联系人'),
        sa.Column('contact_phone', sa.String(length=20), nullable=True, comment='联系电话'),
        sa.Column('company_name', sa.String(length=50), nullable=True, comment='企业名称'),
        sa.Column('license_number', sa.String(length=30), nullable=True, comment='统一社会信用代码'),
        sa.Column('address', sa.String(length=200), nullable=True, comment='地址'),
        sa.Column('intro', sa.String(length=200), nullable=True, comment='企业简介'),
        sa.Column('domain', sa.String(length=200), nullable=True, comment='域名'),
        sa.Column('remark', sa.String(length=200), nullable=True, comment='备注'),
        sa.Column('package_id', sa.BigInteger(), nullable=True, comment='租户套餐编号'),
        sa.Column('expire_time', sa.DateTime(), nullable=True, comment='过期时间'),
        sa.Column('account_count', sa.Integer(), server_default=sa.text('-1'), comment='用户数量（-1不限制）'),
        sa.Column('status', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='租户状态（0正常 1停用）'),
        sa.Column('del_flag', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='删除标志（0代表存在 1代表删除）'),
        sa.Column('create_dept', sa.BigInteger(), nullable=True, comment='创建部门'),
        sa.Column('create_by', sa.BigInteger(), nullable=True, comment='创建者'),
        sa.Column('create_time', sa.DateTime(), nullable=True, comment='创建时间'),
        sa.Column('update_by', sa.BigInteger(), nullable=True, comment='更新者'),
        sa.Column('update_time', sa.DateTime(), nullable=True, comment='更新时间'),
        sa.PrimaryKeyConstraint('id'),
        comment='租户表'
    )

    # 租户套餐表
    op.create_table('sys_tenant_package',
        sa.Column('package_id', sa.BigInteger(), nullable=False, comment='租户套餐id'),
        sa.Column('package_name', sa.String(length=20), nullable=True, comment='套餐名称'),
        sa.Column('menu_ids', sa.String(length=3000), nullable=True, comment='关联菜单id'),
        sa.Column('remark', sa.String(length=200), nullable=True, comment='备注'),
        sa.Column('menu_check_strictly', sa.Integer(), server_default=sa.text('1'), comment='菜单树选择项是否关联显示'),
        sa.Column('status', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='状态（0正常 1停用）'),
        sa.Column('del_flag', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='删除标志（0代表存在 1代表删除）'),
        sa.Column('create_dept', sa.BigInteger(), nullable=True, comment='创建部门'),
        sa.Column('create_by', sa.BigInteger(), nullable=True, comment='创建者'),
        sa.Column('create_time', sa.DateTime(), nullable=True, comment='创建时间'),
        sa.Column('update_by', sa.BigInteger(), nullable=True, comment='更新者'),
        sa.Column('update_time', sa.DateTime(), nullable=True, comment='更新时间'),
        sa.PrimaryKeyConstraint('package_id'),
        comment='租户套餐表'
    )

    # 部门表
    op.create_table('sys_dept',
        sa.Column('dept_id', sa.BigInteger(), nullable=False, comment='部门id'),
        sa.Column('tenant_id', sa.String(length=20), server_default=sa.text("'000000'"), nullable=False, comment='租户编号'),
        sa.Column('parent_id', sa.BigInteger(), server_default=sa.text('0'), comment='父部门id'),
        sa.Column('ancestors', sa.String(length=500), server_default=sa.text("''"), comment='祖级列表'),
        sa.Column('dept_name', sa.String(length=30), server_default=sa.text("''"), comment='部门名称'),
        sa.Column('dept_category', sa.String(length=100), nullable=True, comment='部门类别编码'),
        sa.Column('order_num', sa.Integer(), server_default=sa.text('0'), comment='显示顺序'),
        sa.Column('leader', sa.BigInteger(), nullable=True, comment='负责人'),
        sa.Column('phone', sa.String(length=11), nullable=True, comment='联系电话'),
        sa.Column('email', sa.String(length=50), nullable=True, comment='邮箱'),
        sa.Column('status', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='部门状态（0正常 1停用）'),
        sa.Column('del_flag', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='删除标志（0代表存在 1代表删除）'),
        sa.Column('create_dept', sa.BigInteger(), nullable=True, comment='创建部门'),
        sa.Column('create_by', sa.BigInteger(), nullable=True, comment='创建者'),
        sa.Column('create_time', sa.DateTime(), nullable=True, comment='创建时间'),
        sa.Column('update_by', sa.BigInteger(), nullable=True, comment='更新者'),
        sa.Column('update_time', sa.DateTime(), nullable=True, comment='更新时间'),
        sa.Column('remark', sa.String(length=500), nullable=True, comment='备注'),
        sa.PrimaryKeyConstraint('dept_id'),
        sa.Index('idx_sys_dept_tenant', 'tenant_id'),
        comment='部门表'
    )

    # 用户表
    op.create_table('sys_user',
        sa.Column('user_id', sa.BigInteger(), nullable=False, comment='用户ID'),
        sa.Column('tenant_id', sa.String(length=20), server_default=sa.text("'000000'"), nullable=False, comment='租户编号'),
        sa.Column('dept_id', sa.BigInteger(), nullable=True, comment='部门ID'),
        sa.Column('user_name', sa.String(length=30), nullable=False, comment='用户账号'),
        sa.Column('nick_name', sa.String(length=30), nullable=False, comment='用户昵称'),
        sa.Column('user_type', sa.String(length=10), server_default=sa.text("'sys_user'"), comment='用户类型（sys_user系统用户）'),
        sa.Column('email', sa.String(length=50), server_default=sa.text("''"), comment='用户邮箱'),
        sa.Column('phonenumber', sa.String(length=11), server_default=sa.text("''"), comment='手机号码'),
        sa.Column('sex', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='用户性别（0男 1女 2未知）'),
        sa.Column('avatar', sa.BigInteger(), nullable=True, comment='头像地址'),
        sa.Column('password', sa.String(length=100), server_default=sa.text("''"), comment='密码'),
        sa.Column('status', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='帐号状态（0正常 1停用）'),
        sa.Column('del_flag', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='删除标志（0代表存在 1代表删除）'),
        sa.Column('login_ip', sa.String(length=128), server_default=sa.text("''"), comment='最后登录IP'),
        sa.Column('login_date', sa.DateTime(), nullable=True, comment='最后登录时间'),
        sa.Column('create_dept', sa.BigInteger(), nullable=True, comment='创建部门'),
        sa.Column('create_by', sa.BigInteger(), nullable=True, comment='创建者'),
        sa.Column('create_time', sa.DateTime(), nullable=True, comment='创建时间'),
        sa.Column('update_by', sa.BigInteger(), nullable=True, comment='更新者'),
        sa.Column('update_time', sa.DateTime(), nullable=True, comment='更新时间'),
        sa.Column('remark', sa.String(length=500), nullable=True, comment='备注'),
        sa.PrimaryKeyConstraint('user_id'),
        sa.Index('idx_sys_user_tenant', 'tenant_id'),
        comment='用户信息表'
    )

    # 岗位表
    op.create_table('sys_post',
        sa.Column('post_id', sa.BigInteger(), nullable=False, comment='岗位ID'),
        sa.Column('tenant_id', sa.String(length=20), server_default=sa.text("'000000'"), nullable=False, comment='租户编号'),
        sa.Column('dept_id', sa.BigInteger(), nullable=False, comment='部门id'),
        sa.Column('post_code', sa.String(length=64), nullable=False, comment='岗位编码'),
        sa.Column('post_category', sa.String(length=100), nullable=True, comment='岗位类别编码'),
        sa.Column('post_name', sa.String(length=50), nullable=False, comment='岗位名称'),
        sa.Column('post_sort', sa.Integer(), nullable=False, comment='显示顺序'),
        sa.Column('status', sa.CHAR(length=1), nullable=False, comment='状态（0正常 1停用）'),
        sa.Column('del_flag', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='删除标志（0代表存在 1代表删除）'),
        sa.Column('create_dept', sa.BigInteger(), nullable=True, comment='创建部门'),
        sa.Column('create_by', sa.BigInteger(), nullable=True, comment='创建者'),
        sa.Column('create_time', sa.DateTime(), nullable=True, comment='创建时间'),
        sa.Column('update_by', sa.BigInteger(), nullable=True, comment='更新者'),
        sa.Column('update_time', sa.DateTime(), nullable=True, comment='更新时间'),
        sa.Column('remark', sa.String(length=500), nullable=True, comment='备注'),
        sa.PrimaryKeyConstraint('post_id'),
        sa.Index('idx_sys_post_tenant', 'tenant_id'),
        comment='岗位信息表'
    )

    # 角色表
    op.create_table('sys_role',
        sa.Column('role_id', sa.BigInteger(), nullable=False, comment='角色ID'),
        sa.Column('tenant_id', sa.String(length=20), server_default=sa.text("'000000'"), nullable=False, comment='租户编号'),
        sa.Column('role_name', sa.String(length=30), nullable=False, comment='角色名称'),
        sa.Column('role_key', sa.String(length=100), nullable=False, comment='角色权限字符串'),
        sa.Column('role_sort', sa.Integer(), nullable=False, comment='显示顺序'),
        sa.Column('data_scope', sa.CHAR(length=1), server_default=sa.text("'1'"), comment='数据范围（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限）'),
        sa.Column('menu_check_strictly', sa.Integer(), server_default=sa.text('1'), comment='菜单树选择项是否关联显示'),
        sa.Column('dept_check_strictly', sa.Integer(), server_default=sa.text('1'), comment='部门树选择项是否关联显示'),
        sa.Column('status', sa.CHAR(length=1), nullable=False, comment='角色状态（0正常 1停用）'),
        sa.Column('del_flag', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='删除标志（0代表存在 1代表删除）'),
        sa.Column('create_dept', sa.BigInteger(), nullable=True, comment='创建部门'),
        sa.Column('create_by', sa.BigInteger(), nullable=True, comment='创建者'),
        sa.Column('create_time', sa.DateTime(), nullable=True, comment='创建时间'),
        sa.Column('update_by', sa.BigInteger(), nullable=True, comment='更新者'),
        sa.Column('update_time', sa.DateTime(), nullable=True, comment='更新时间'),
        sa.Column('remark', sa.String(length=500), nullable=True, comment='备注'),
        sa.PrimaryKeyConstraint('role_id'),
        sa.Index('idx_sys_role_tenant', 'tenant_id'),
        comment='角色信息表'
    )

    # 菜单权限表
    op.create_table('sys_menu',
        sa.Column('menu_id', sa.BigInteger(), nullable=False, comment='菜单ID'),
        sa.Column('menu_name', sa.String(length=50), nullable=False, comment='菜单名称'),
        sa.Column('parent_id', sa.BigInteger(), server_default=sa.text('0'), comment='父菜单ID'),
        sa.Column('order_num', sa.Integer(), server_default=sa.text('0'), comment='显示顺序'),
        sa.Column('path', sa.String(length=200), server_default=sa.text("''"), comment='路由地址'),
        sa.Column('component', sa.String(length=255), nullable=True, comment='组件路径'),
        sa.Column('query_param', sa.String(length=255), nullable=True, comment='路由参数'),
        sa.Column('is_frame', sa.Integer(), server_default=sa.text('1'), comment='是否为外链（0是 1否）'),
        sa.Column('is_cache', sa.Integer(), server_default=sa.text('0'), comment='是否缓存（0缓存 1不缓存）'),
        sa.Column('menu_type', sa.CHAR(length=1), server_default=sa.text("''"), comment='菜单类型（M目录 C菜单 F按钮）'),
        sa.Column('visible', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='显示状态（0显示 1隐藏）'),
        sa.Column('status', sa.CHAR(length=1), server_default=sa.text("'0'"), comment='菜单状态（0正常 1停用）'),
        sa.Column('perms', sa.String(length=100), nullable=True, comment='权限标识'),
        sa.Column('icon', sa.String(length=100), server_default=sa.text("'#'"), comment='菜单图标'),
        sa.Column('create_dept', sa.BigInteger(), nullable=True, comment='创建部门'),
        sa.Column('create_by', sa.BigInteger(), nullable=True, comment='创建者'),
        sa.Column('create_time', sa.DateTime(), nullable=True, comment='创建时间'),
        sa.Column('update_by', sa.BigInteger(), nullable=True, comment='更新者'),
        sa.Column('update_time', sa.DateTime(), nullable=True, comment='更新时间'),
        sa.Column('remark', sa.String(length=500), nullable=True, comment='备注'),
        sa.PrimaryKeyConstraint('menu_id'),
        comment='菜单权限表'
    )

    # 用户角色关联表
    op.create_table('sys_user_role',
        sa.Column('user_id', sa.BigInteger(), nullable=False, comment='用户ID'),
        sa.Column('role_id', sa.BigInteger(), nullable=False, comment='角色ID'),
        sa.PrimaryKeyConstraint('user_id', 'role_id'),
        comment='用户和角色关联表'
    )

    # 用户岗位关联表
    op.create_table('sys_user_post',
        sa.Column('user_id', sa.BigInteger(), nullable=False, comment='用户ID'),
        sa.Column('post_id', sa.BigInteger(), nullable=False, comment='岗位ID'),
        sa.PrimaryKeyConstraint('user_id', 'post_id'),
        comment='用户与岗位关联表'
    )

    # 角色菜单关联表
    op.create_table('sys_role_menu',
        sa.Column('role_id', sa.BigInteger(), nullable=False, comment='角色ID'),
        sa.Column('menu_id', sa.BigInteger(), nullable=False, comment='菜单ID'),
        sa.PrimaryKeyConstraint('role_id', 'menu_id'),
        comment='角色和菜单关联表'
    )

    # 系统配置表
    op.create_table('sys_config',
        sa.Column('config_id', sa.BigInteger(), nullable=False, comment='参数主键'),
        sa.Column('tenant_id', sa.String(length=20), server_default=sa.text("'000000'"), nullable=False, comment='租户编号'),
        sa.Column('config_name', sa.String(length=100), server_default=sa.text("''"), comment='参数名称'),
        sa.Column('config_key', sa.String(length=100), server_default=sa.text("''"), comment='参数键名'),
        sa.Column('config_value', sa.String(length=500), server_default=sa.text("''"), comment='参数键值'),
        sa.Column('config_type', sa.CHAR(length=1), server_default=sa.text("'N'"), comment='系统内置（Y是 N否）'),
        sa.Column('create_dept', sa.BigInteger(), nullable=True, comment='创建部门'),
        sa.Column('create_by', sa.BigInteger(), nullable=True, comment='创建者'),
        sa.Column('create_time', sa.DateTime(), nullable=True, comment='创建时间'),
        sa.Column('update_by', sa.BigInteger(), nullable=True, comment='更新者'),
        sa.Column('update_time', sa.DateTime(), nullable=True, comment='更新时间'),
        sa.Column('remark', sa.String(length=500), nullable=True, comment='备注'),
        sa.PrimaryKeyConstraint('config_id'),
        sa.Index('idx_sys_config_tenant', 'tenant_id'),
        comment='参数配置表'
    )

    # 字典类型表
    op.create_table('sys_dict_type',
        sa.Column('dict_id', sa.BigInteger(), nullable=False, comment='字典主键'),
        sa.Column('tenant_id', sa.String(length=20), server_default=sa.text("'000000'"), nullable=False, comment='租户编号'),
        sa.Column('dict_name', sa.String(length=100), server_default=sa.text("''"), comment='字典名称'),
        sa.Column('dict_type', sa.String(length=100), server_default=sa.text("''"), comment='字典类型'),
        sa.Column('create_dept', sa.BigInteger(), nullable=True, comment='创建部门'),
        sa.Column('create_by', sa.BigInteger(), nullable=True, comment='创建者'),
        sa.Column('create_time', sa.DateTime(), nullable=True, comment='创建时间'),
        sa.Column('update_by', sa.BigInteger(), nullable=True, comment='更新者'),
        sa.Column('update_time', sa.DateTime(), nullable=True, comment='更新时间'),
        sa.Column('remark', sa.String(length=500), nullable=True, comment='备注'),
        sa.PrimaryKeyConstraint('dict_id'),
        sa.UniqueConstraint('tenant_id', 'dict_type'),
        sa.Index('idx_sys_dict_type_tenant', 'tenant_id'),
        comment='字典类型表'
    )

    # 字典数据表
    op.create_table('sys_dict_data',
        sa.Column('dict_code', sa.BigInteger(), nullable=False, comment='字典编码'),
        sa.Column('tenant_id', sa.String(length=20), server_default=sa.text("'000000'"), nullable=False, comment='租户编号'),
        sa.Column('dict_sort', sa.Integer(), server_default=sa.text('0'), comment='字典排序'),
        sa.Column('dict_label', sa.String(length=100), server_default=sa.text("''"), comment='字典标签'),
        sa.Column('dict_value', sa.String(length=100), server_default=sa.text("''"), comment='字典键值'),
        sa.Column('dict_type', sa.String(length=100), server_default=sa.text("''"), comment='字典类型'),
        sa.Column('css_class', sa.String(length=100), nullable=True, comment='样式属性（其他样式扩展）'),
        sa.Column('list_class', sa.String(length=100), nullable=True, comment='表格回显样式'),
        sa.Column('is_default', sa.CHAR(length=1), server_default=sa.text("'N'"), comment='是否默认（Y是 N否）'),
        sa.Column('create_dept', sa.BigInteger(), nullable=True, comment='创建部门'),
        sa.Column('create_by', sa.BigInteger(), nullable=True, comment='创建者'),
        sa.Column('create_time', sa.DateTime(), nullable=True, comment='创建时间'),
        sa.Column('update_by', sa.BigInteger(), nullable=True, comment='更新者'),
        sa.Column('update_time', sa.DateTime(), nullable=True, comment='更新时间'),
        sa.Column('remark', sa.String(length=500), nullable=True, comment='备注'),
        sa.PrimaryKeyConstraint('dict_code'),
        sa.Index('idx_sys_dict_data_tenant', 'tenant_id'),
        comment='字典数据表'
    )


def downgrade() -> None:
    # 删除所有表
    op.drop_table('sys_dict_data')
    op.drop_table('sys_dict_type')
    op.drop_table('sys_config')
    op.drop_table('sys_role_menu')
    op.drop_table('sys_user_post')
    op.drop_table('sys_user_role')
    op.drop_table('sys_menu')
    op.drop_table('sys_role')
    op.drop_table('sys_post')
    op.drop_table('sys_user')
    op.drop_table('sys_dept')
    op.drop_table('sys_tenant_package')
    op.drop_table('sys_tenant')